package com.zwj.controller;

import com.zwj.pojo.Backend;
import com.zwj.service.ByBackendService;
import com.zwj.service.ByUserService;
import com.zwj.utils.Constants;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
public class UserBackendLoginController {

    @Resource
    private ByUserService byUserService;
    @Resource
    private ByBackendService byBackendService;
    @PostMapping("/manager/dologin")
    public String logo(String userCode, String userPassword, Model model, HttpSession session) throws Exception {
        Backend logo = byBackendService.logo(userCode, userPassword);
        if (null == logo) {
            model.addAttribute("error", "用户名或密码错误!!");
            return "backendlogin";
        } else {
            System.out.println("logo = " + logo);
            session.setAttribute(Constants.USER_SESSION,logo);
            return "/backend/main";
        }

    }
  @GetMapping("/manager/login")
    public String login() {
        return "backendlogin";
    }

    @RequestMapping(value="/manager/logout")
    public String logout(HttpSession session){
        //清除session
        session.removeAttribute(Constants.USER_SESSION);
        return "backendlogin";
    }
}

